package com.shipxpress.register.view;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import com.shipxpress.register.model.User;

public class UserForm extends ActionForm {

    private String name = null;
    private String address = null;
    private String emailAddress = null;
    private Integer age = null;

    private String forwardAction = null;

    public UserForm() {
    }

    public UserForm(User user) {
        this.name = user.getName();
        this.address = user.getAddress();
        this.emailAddress = user.getEmailAddress();
        this.age = user.getAge();
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return this.address;
    }


    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getEmailAddress() {
        return this.emailAddress;
    }

    public String getForwardAction() {
        return forwardAction;
    }

    public void setForwardAction(String forwardAction) {
        this.forwardAction = forwardAction;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * Reset all properties to their default values.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.name = null;
        this.address = null;
        this.emailAddress = null;
        this.age = null;
        this.forwardAction = null;
    }

    /**
     * Reset all properties to their default values.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     * @return errors
     */
    public ActionErrors validate(
            ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();

        if (request.getParameter("forwardAction").equalsIgnoreCase("done") ||
                request.getParameter("forwardAction").equalsIgnoreCase("index")) {
            return errors;
        }
        if (getName() == null || getName().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
        }
        if (getAddress() == null || getAddress().length() < 1) {
            errors.add("address", new ActionMessage("error.address.required"));
        }
        if (getEmailAddress() == null || getEmailAddress().length() < 1) {
            errors.add("emailaddress", new ActionMessage("error.emailaddress.required"));
        }

        if (getAge() == null || getAge() < 1) {
            errors.add("age", new ActionMessage("error.age.required"));
        }

        return errors;
    }

    public void updateData(User user) {
        user.setName(this.getName());
        user.setAddress(this.getAddress());
        user.setEmailAddress(this.getEmailAddress());
        user.setAge(this.getAge());
    }
}
